<?php
namespace Home\Controller;
use Think\Controller;
class ReferralController extends Controller {
    /**
     * 转诊患者
     */
    public function index() {
        $m_referral = M('referral');
        $m_patient = M('patient');

        if(IS_POST){
            if(I('hospital_now') == is_login()){
                $return_data     = array('code' => -3,'msg' => '不能对自己转诊！');
                $this->ajaxReturn($return_data,'json');
            }
            $data['hospital_pre']   = is_login();
            $data['hospital_now']   = I('hospital_now');
            $data['pid']            = I('pid');
            $data['pname']          = I('pname');
            $pinfo   = $m_patient->find($data['pid']);
            $data['cateid']         = $pinfo['cateid'];
            $data['type']           = I('type');
            $data['remarks']        = I('remarks');
            $data['addtime']        = date('Y-m-d H:i:s');
            $where['pid']           = $data['pid'];
            $where['hospital_now']  = $data['hospital_now'];
            $time = date('Y-m-d H:i:s',time()-1800);
            $where['addtime']       = array('gt',$time);
            $info   = $m_referral->where($where)->find();
            //echo $m_referral->getLastSql();die;
            if($info){
                $return_data     = array('code' => -2,'msg' => '该患者已经转诊！');
                $this->ajaxReturn($return_data,'json');
            }
            $add    = $m_referral->add($data);
            if($add){
                $return_data     = array('code' => 0,'msg' => '转诊成功！','url'=>U('User/referrallist'));
                $this->ajaxReturn($return_data,'json');
            }else{
                $return_data     = array('code' => -1,'msg' => '转诊失败！');
                $this->ajaxReturn($return_data,'json');
            }
        };
        $this->display();
    }

    /**
     * 医院列表
     */
    function hospitallist() {
        //省份、名称搜索
        $M_hospital     = M('hospital');
        $M_province     = M('province');
        $provincelist   = $M_province->select();
        $uid            = is_login();
        $pagesize	    = '1';
        if(IS_POST){
            $where      = array();
            if(I('post.province_id')){
                $province_id            = I('post.province_id');
                $where['province_id']   = $province_id;
            }else{
                $where['province_id']   = $provincelist[0]['id'];
            }
            if(I('post.name')){
                $name               = I('post.name');
                $where['name']      = array('like', "%$name%");
            }
            $page = I('post.page');
            $start = ($page-1)*$pagesize;
            $list      = $M_hospital->where($where)->where("id !=".$uid)->limit($start,$pagesize)->select();
            if($list){
                $return_data     = array('code' => 0,'data' => $list,'page'=>$page+1);
                $this->ajaxReturn($return_data,'json');
            }else{
                $return_data     = array('code' => -1,'msg' => '没有更多的医院了');
                $this->ajaxReturn($return_data,'json');
            }
        }
        $this->assign('provincelist',$provincelist);
        $this->display();
    }
    /**
     * 患者列表
     */
    function patientlist() {
        //疾病类别、名称搜索
        $M_hospital = M('patient');
        $M_category = M('category');
        $catelist   = $M_category->select();
        $pagesize	= '1';
        if(IS_POST){
            $where  = array();
            if(I('post.cateid')){
                $cateid             = I('post.cateid');
                $where['cateid']    = $cateid;
            }
            if(I('post.name')){
                $name               = I('post.name');
                $where['name']      = array('like', "%$name%");
            }
            $where['hid'] = is_login();
            //print_r($where);die;
            $page       = I('post.page');
            $start      = ($page-1)*$pagesize;
            $list       = $M_hospital->where($where)
                                        ->join('ms_category as c on ms_patient.cateid=c.id')
                                        ->order('ms_patient.addtime desc')
                                        ->limit($start,$pagesize)
                                        ->field('ms_patient.id,ms_patient.name,ms_patient.addtime,c.title')
                                        ->select();
            //echo $M_hospital->getLastSql();
            if($list){
                $return_data     = array('code' => 0,'data' => $list,'page'=>$page+1);
                $this->ajaxReturn($return_data,'json');
            }else{
                $return_data     = array('code' => -1,'msg' => '没有符合条件的患者');
                $this->ajaxReturn($return_data,'json');
            }
        }
        $this->assign('catelist',$catelist);
        $this->display();
    }
    /**
     * 医院详情
     */
    public function hos_detail(){
        $id = I('get.id');
        if($id){
            $M_hospital     = M('hospital');
            $hospitalinfo   = $M_hospital->find($id);
            $this->assign('info',$hospitalinfo);
        }
        $this->display();
    }


    //多图上传页面
    public function test(){
        $this->display();

    }

}